package net.service;

import com.zjitc.domain.Mail;
import net.listener.MailQueueMessageListener;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

@Service
public class MailService {

    public static Logger logger = Logger.getLogger(MailQueueMessageListener.class);

    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private SimpleMailMessage simpleMailMessage;
    @Autowired
    private ThreadPoolTaskExecutor threadPool;

    /**
     * 发送邮件
     * @param mail
     */
    public void sendMail(final Mail mail){
        threadPool.execute(new Runnable() {
            public void run() {
                try {
                    simpleMailMessage.setFrom(simpleMailMessage.getFrom());
                    simpleMailMessage.setTo(mail.getTo());
                    simpleMailMessage.setSubject(mail.getSubject());
                    simpleMailMessage.setText(mail.getContent());
                    mailSender.send(simpleMailMessage);
                   logger.debug("mail send ok by zhaojingjing");
                } catch (MailException e) {
                    e.printStackTrace();
                    throw e;
                }
            }
        });
        return;
    }
}
